{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前是第1次猜\n",
      "范围: 1~100\n",
      "当前是第2次猜\n",
      "范围: 1~77\n",
      "输入错误,按下[Control + C]结束游戏\n",
      "当前是第3次猜\n",
      "范围: 1~77\n",
      "当前是第4次猜\n",
      "范围: 49~77\n",
      "输入错误,按下[Control + C]结束游戏\n",
      "当前是第5次猜\n",
      "范围: 49~77\n",
      "当前是第6次猜\n",
      "范围: 51~77\n",
      "输入错误,按下[Control + C]结束游戏\n",
      "当前是第7次猜\n",
      "范围: 51~77\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: ''",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[18], line 18\u001b[0m\n\u001b[1;32m     16\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m当前是第\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mguestCounter\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m次猜\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m     17\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m范围: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrangeMin\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m~\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrangeMax\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 18\u001b[0m guestNum \u001b[38;5;241m=\u001b[39m \u001b[43minputInt\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m请输入你要猜的数字\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m     19\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m guestNum \u001b[38;5;241m==\u001b[39m targetNum:\n\u001b[1;32m     20\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m猜中了: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mguestNum\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
      "Cell \u001b[0;32mIn[18], line 6\u001b[0m, in \u001b[0;36minputInt\u001b[0;34m(inPrompt)\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21minputInt\u001b[39m(inPrompt) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28mint\u001b[39m:\n\u001b[0;32m----> 6\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43minPrompt\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: ''"
     ]
    }
   ],
   "source": [
    "# 猜数字练习\n",
    "import random\n",
    "\n",
    "\n",
    "def inputInt(inPrompt) -> int:\n",
    "    return int(input(inPrompt))\n",
    "\n",
    "\n",
    "rangeMin = 1\n",
    "rangeMax = 100\n",
    "targetNum = random.randint(rangeMin, rangeMax)\n",
    "guestNum = -1\n",
    "guestCounter = 0\n",
    "while True:\n",
    "    guestCounter += 1\n",
    "    print(f\"当前是第{guestCounter}次猜\")\n",
    "    print(f\"范围: {rangeMin}~{rangeMax}\")\n",
    "    guestNum = inputInt(\"请输入你要猜的数字\")\n",
    "    if guestNum == targetNum:\n",
    "        print(f\"猜中了: {guestNum}\")\n",
    "        break\n",
    "    elif rangeMin <= guestNum and guestNum < targetNum:\n",
    "        rangeMin = guestNum + 1\n",
    "    elif guestNum <= rangeMax and targetNum < guestNum:\n",
    "        rangeMax = guestNum - 1\n",
    "    else:\n",
    "        print(f\"输入错误,按下[Control + C]结束游戏\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
